package com.greedy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Yanxy
 * @Date: Created in:2022-12-25
 * Description: 用户登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * pre：前置拦截。 进入controller执行，进行校验
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断是否需要拦截（ThreadLocal中是否有用户）
        if (null == UserHolder.getUser()) {
            // 401:用户未授权
            response.setStatus(401);
            return false;
        }
        return true;
    }

}
